/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab6.Home;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author THIEN
 */
public class ManageList {

    public ManageList() {
    }
    ArrayList<Computer> ListComputer = new ArrayList<Computer>();
    ArrayList<Teacher> ListTeacher = new ArrayList<Teacher>();
    ArrayList<Student> ListStudent = new ArrayList<Student>();
    InputStreamReader ISR = new InputStreamReader(System.in);
    BufferedReader BR = new BufferedReader(ISR);

    public void addComputer(String Object) throws IOException {

        String answer = "y";
        if (Object.equals("Computer")) {
            while (answer.equals("y") || answer.equals("Y")) {
                //Add computer
                System.out.println("Computer's infomation");
                System.out.print("Input computer's id: ");
                int id = Integer.parseInt(BR.readLine());

                System.out.print("\nInput computer's name: ");
                String name = BR.readLine();

                System.out.print("\nInput computer's price: ");
                double p = Double.parseDouble(BR.readLine());
                Double price = new Double(p);

                //add into list
                Computer computer = new Computer(id, name, price);
                ListComputer.add(computer);

                //ask for continue or not ?
                System.out.println("Are you want to continue (Y/N) ?");
                answer = BR.readLine();
            }
            Demo.subMenu("Computer");
        }

        if (Object.equals("Teacher")) {
            while (answer.equals("y") || answer.equals("Y")) {
                //Add computer
                System.out.println("Teacher's infomation");
                System.out.print("Input teacher's id: ");
                int id = Integer.parseInt(BR.readLine());

                System.out.print("\nInput teacher's name: ");
                String name = BR.readLine();

                System.out.print("\nInput teacher's age: ");
                int age = Integer.parseInt(BR.readLine());

                //add into list
                Teacher teacher = new Teacher(id, name, age);
                ListTeacher.add(teacher);

                //ask for continue or not ?
                System.out.println("Are you want to continue (Y/N) ?");
                answer = BR.readLine();
            }
            Demo.subMenu("Teacher");
        }
        if (Object.equals("Student")) {
            while (answer.equals("y") || answer.equals("Y")) {
                //Add computer
                System.out.println("Student's infomation");
                System.out.print("Input Student's id: ");
                int id = Integer.parseInt(BR.readLine());

                System.out.print("\nInput student's name: ");
                String name = BR.readLine();

                System.out.print("\nInput student's class: ");
                String classes = BR.readLine();

                //add into list
                Student student = new Student(id, name, classes);
                ListStudent.add(student);

                //ask for continue or not ?
                System.out.println("Are you want to continue (Y/N) ?");
                answer = BR.readLine();
            }
            Demo.subMenu("Student");
        }
    }

    public void Display(String Object) throws IOException {
        if (Object.equals("Computer")) {
            for (Computer computer : ListComputer) {
                System.out.println(computer);
            }
        }
        if (Object.equals("Teacher")) {
            for (Teacher teacher : ListTeacher) {
                System.out.println(teacher);
            }
        }
        if (Object.equals("Student")) {
            for (Student student : ListStudent) {
                System.out.println(student);

            }
        }
        Demo.Menu();

    }

    public void Search(String Object) throws IOException {

        /**
         * Seach computer's name !
         * @author THIEN
         */
        if (Object.equals("Computer")) {
            System.out.println("Input the computer's name you want to search: ");
            String searchObj = BR.readLine();
            Pattern pattObj = Pattern.compile(searchObj, Pattern.CASE_INSENSITIVE);

            //search each computer in computer list
            for (Computer computer : ListComputer) {
                String temp = computer.getName();
                Matcher matcher = pattObj.matcher(temp);
                //loop for search found

                while (matcher.find()) {
                    System.out.println("Found");
                    System.out.println(computer);
                }
            }
            Demo.subMenu("Computer");
        }

        /**
         * Seach Teacher's name
         * @author THIEN
         */
        if (Object.equals("Teacher")) {
            System.out.println("Input the Teacher's name you want to search: ");
            String searchObj = BR.readLine();
            Pattern pattObj = Pattern.compile(searchObj, Pattern.CASE_INSENSITIVE);

            //search each teacher in teacher list
            for (Teacher teacher : ListTeacher) {
                String temp = teacher.getName();
                Matcher matcher = pattObj.matcher(temp);
                //loop for search found

                while (matcher.find()) {
                    System.out.println("Found");
                    System.out.println(teacher);
                }
            }
            Demo.subMenu("Teacher");
        }

        /**
         * Search Student's name
         * @author THIEN
         */
        if (Object.equals("Student")) {
            System.out.println("Input the Student's name you want to search: ");
            String searchObj = BR.readLine();
            Pattern pattObj = Pattern.compile(searchObj, Pattern.CASE_INSENSITIVE);

            //search each Student in student list

            for (Student student : ListStudent) {
                String temp = student.getName();
                Matcher matcher = pattObj.matcher(temp);

                //loop for search found

                while (matcher.find()) {
                    System.out.println("Found");
                    System.out.println(student);
                }
            }
            Demo.subMenu("Student");
        }
    }
}
